import React, { useState } from 'react';
import { Card, Form, Spin, message, Button } from 'antd';
import { withRouter, connect } from 'umi';

import BottomAction from '@/components/BottomAction';
import { isSuccess } from '@/utils/request';
import PageHeaderWrapper from '@/components/PageHeaderWrapper';

import { insuranceHousingFundService } from '@/services/salaryManage/salaryCalculateService';
import { closeCurrentAndOpen } from '@/pages/SalaryManage/utils/pageNavigateUtil';
import {
    getAreaItemPath,
    getFullCodeByAreaItemPath,
    getFullNameByAreaItemPath,
    getNameByAreaItem
} from '@/pages/SalaryManage/components/CityCascader';
import MedicalSocialSecurityForm from './components/MedicalSocialSecurityForm';

/**
 * 新增险金档案
 * @param form
 * @param organizationCode
 * @returns {JSX.Element}
 * @constructor
 */
const MedicalSocialSecurityAdd = ({ form, organizationCode }) => {
    const [loading, setLoading] = useState(false);

    // 重置
    const onReset = () => {
        form.resetFields();
    }

    // 保存时的回调
    const onSave = () => {
        form.validateFieldsAndScroll((errors, values) => {
            if (errors) {
                return;
            }

            if (values.limitUpperAmt && values.limitDownAmt && values.limitUpperAmt < values.limitDownAmt) {
                message.error('【下限】不能大于【上限】');
                return;
            }

            const { v_areaItemArray: areaItemArray, v_city, ...postData } = values;

            if (areaItemArray.length > 1) {
                const lastAreaItem = areaItemArray[areaItemArray.length - 1];
                postData.socialCityCode = lastAreaItem.code;
                postData.socialCityName = getNameByAreaItem(lastAreaItem);

                const areaItemPath = getAreaItemPath(lastAreaItem.code);
                postData.socialCityCodePath = getFullCodeByAreaItemPath(areaItemPath);
                postData.socialCityNamePath = getFullNameByAreaItemPath(areaItemPath);
                postData.organizationCode = organizationCode;
            }

            setLoading(true);
            insuranceHousingFundService
                .saveNewMedicalSocial(postData)
                .then(response => {
                    setLoading(false);
                    if (!isSuccess(response)) {
                        message.error(response.msg);
                        return;
                    }
                    message.success('保存成功！');
                    closeCurrentAndOpen({
                        pathname: '/finance/salary/calculate/insurancefundfile/medicalsocialsecurity',
                        query: {
                            socialCityCode: response.data,
                            organizationCode
                        },
                        state: {
                            tabRoute: true,
                            tabRefresh: true
                        }
                    });
                })
                .catch(() => {
                    setLoading(false);
                });
        });
    };

    return (
        <PageHeaderWrapper type="success">
            <Card bordered={false} title="新增险金档案">
                <Spin spinning={loading}>
                    <div style={{ width: 532 }}>
                        <MedicalSocialSecurityForm form={form} model={{}} organizationCode={organizationCode} />
                    </div>
                    <BottomAction>
                        <Button onClick={onReset}>重置</Button>
                        <Button onClick={onSave} type="primary">
                            确定
                        </Button>
                    </BottomAction>
                </Spin>
            </Card>
        </PageHeaderWrapper>
    );
};

export default withRouter(
    connect((state, { location }) => ({
        organizationCode: location?.query?.organizationCode,
        effect: location?.query?.effect
    }))(Form.create({})(MedicalSocialSecurityAdd))
);
